package com.srit.yoggsaron.support;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.srit.yoggsaron.common.JsonQuery;
import com.srit.yoggsaron.db.TaskMapper;
import com.srit.yoggsaron.db.TaskViewMapper;
import com.srit.yoggsaron.db.model.Task;
import com.srit.yoggsaron.db.model.TaskView;

@Service
public class TaskSupport {

	private final Logger logger = LoggerFactory.getLogger(TaskSupport.class);
	
	@Autowired
	private TaskMapper _taskMapper;
	
	@Autowired
	private TaskViewMapper _taskViewMapper;
	
	
	public  List<TaskView>  queryView (   JsonQuery jsonQuery   )throws Exception {
		return  _taskViewMapper.query(   jsonQuery );  
	}
	public  List<TaskView>  pageView (   JsonQuery jsonQuery   )throws Exception {
		return  _taskViewMapper.page(   jsonQuery );  
	}
	
	
	public  List<Task>  query (   JsonQuery jsonQuery   )throws Exception {
		return  _taskMapper.query(   jsonQuery );  
	}
	public  List<Task>  page (   JsonQuery jsonQuery   )throws Exception {
		return  _taskMapper.page(   jsonQuery );  
	}
	public  Long  count (   JsonQuery jsonQuery   )throws Exception {
		return  _taskMapper.count(   jsonQuery );  
	}
	
	
	
	public  List<TaskView>  viewPage (   JsonQuery jsonQuery   )throws Exception {
		return  _taskViewMapper.page(   jsonQuery );  
	}
	public  Long  viewCount (   JsonQuery jsonQuery   )throws Exception {
		return  _taskViewMapper.count(   jsonQuery );  
	}
	 
	
	
	
	public  Long  insert (   Task task  )throws Exception {
		if(task==null )
			throw new Exception("要增加的用户为空 ");
		task.setTaskId("Task-"+UUID.randomUUID().toString());
		if( task.getTaskStatus()==null)
			task.setTaskStatus(Task.TASK_STATUS_WAITING +"" );
		if(task.getCreateTime()==null )
			task.setCreateTime(new Date() );
		task.setTaskType(1);
		
		
		if(task.getUserId()==null )
			task.setStartTime(null );
		if(task.getUserId()!=null)
		{
			task.setStartTime( new Date() );
			task.setTaskStatus(Task.TASK_STATUS_DOING +"" );
		}
		task.setUpdateTime(new Date() );
		return _taskMapper.insert( task );
	}
	
	public  Long  delete (   JsonQuery jsonQuery   )throws Exception {
		if(jsonQuery==null)
			throw new Exception("要删除的用户条件为空");
		Long  result = _taskMapper.delete(  jsonQuery );
		return  result ;
	}
	 
	public  Long  update (  Task task)throws Exception {
		if(task==null|| task.getTaskId()  ==null)
			throw new Exception("要修改的用户为空或用户ID为空");
		return _taskMapper.update( task );
	}
	
}
